package com.yh.autobio;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * 应用程序配置类：用于保存用户相关信息及设置
 *
 * @author 火蚁 (http://my.oschina.net/LittleDY)
 * @version 1.0
 * @created 2014-04-21
 */
public class AppConfig {
    public static final String key = "yhjmzcrm1";
    public static final int HTTP_CONNECT_TIMEOUT = 60000;
    public static final int HTTP_READ_TIMEOUT = 120000;//2分钟
    public static final String INTENT_ACTION_LOGOUT = "com.yh.foodbarcode.LOGOUT";
    public static final String SCN_CUST_ACTION_SCODE = "android.intent.action.SCANRESULT";
    public static final String SCN_CUST_EX_SCODE = "value";
    public static final String INTENT_ACTION_NOTICE = "com.yh.foodbarcode.action.APPWIDGET_UPDATE";
    public static final String PROP_KEY_PRIVATE_TOKEN = "private_token";
    private final static String APP_CONFIG = "config";
    public final static String KEY_DOUBLE_CLICK_EXIT = "doubleclick";
    public final static String CONF_APP_UNIQUEID = "APP_UNIQUEID";
    public final static String CONF_COOKIE = "cookie";
    public final static String CONF_LOAD_IMAGE = "perf_loadimage";
    public static final String USER_SELECT = "intent_user_select_action";
    public final static String CONF_HTTPS_LOGIN = "perf_httpslogin";

    public final static String CONF_RECEIVENOTICE = "perf_receivenotice";

    public final static String CONF_VOICE = "perf_voice";
    public final static String PWD = "pwd";
    public final static String PRIVATE_TOKEN = "private_token";;
    public static final String KEY_NOTE_DRAFT = "KEY_NOTE_DRAFT";
    public final static String CONF_FRIST_START = "isFristStart";
    public static final String UPWORK = "intent_update_workdo_info";
    public static final String UPDATA_BODY = "update_body";
    public static final String SALE_OUT_SAVE = "SALE_OUT_SAVE";

    //默认存放图片的路径
    public final static String DEFAULT_SAVE_IMAGE_PATH = Environment
            .getExternalStorageDirectory()
            + File.separator
            + "YHCRM"
            + File.separator;

    //默认存放图片的路径
    public final static String DEFAULT_SAVE_FILE_PATH = Environment
            .getExternalStorageDirectory()
            + File.separator
            + "YHCRM"
            + File.separator
            + "download"
            + File.separator;

    private Context mContext;
    private static AppConfig appConfig;

    public static AppConfig getAppConfig(Context context) {
        if (appConfig == null) {
            appConfig = new AppConfig();
            appConfig.mContext = context;
        }
        return appConfig;
    }

    /**
     * 获取Preference设置
     */
    public static SharedPreferences getSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    /**
     * 是否加载显示文章图片
     */
    public static boolean isLoadImage(Context context) {
        return getSharedPreferences(context).getBoolean(CONF_LOAD_IMAGE, true);
    }

    /**
     * 获得用户的token
     *
     * @return
     */
    public String getPrivateToken() {
        return get(PROP_KEY_PRIVATE_TOKEN);
    }

    public String get(String key) {
        Properties props = get();
        return (props != null) ? props.getProperty(key) : null;
    }

    public Properties get() {
        FileInputStream fis = null;
        Properties props = new Properties();
        try {
            // 读取files目录下的config
            // fis = activity.openFileInput(APP_CONFIG);

            // 读取app_config目录下的config
            File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
            fis = new FileInputStream(dirConf.getPath() + File.separator
                    + APP_CONFIG);

            props.load(fis);
        } catch (Exception e) {
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        return props;
    }

    private void setProps(Properties p) {
        FileOutputStream fos = null;
        try {
            // 把config建在files目录下
            // fos = activity.openFileOutput(APP_CONFIG, Context.MODE_PRIVATE);

            // 把config建在(自定义)app_config的目录下
            File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
            File conf = new File(dirConf, APP_CONFIG);
            fos = new FileOutputStream(conf);

            p.store(fos, null);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }

    public void set(Properties ps) {
        Properties props = get();
        props.putAll(ps);
        setProps(props);
    }

    public void set(String key, String value) {
        Properties props = get();
        props.setProperty(key, value);
        setProps(props);
    }

    public void remove(String... key) {
        Properties props = get();
        for (String k : key)
            props.remove(k);
        setProps(props);
    }
}
